package com.ky.serialport.socket;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.ky.serialportpackage.R;
import com.ky.serialport.service.SocketService;
import com.ky.serialport.socket.event.ConnectBreakEvent;
import com.ky.serialport.socket.event.ConnectFaileEvent;
import com.ky.serialport.socket.event.ConnectSuccessEvent;
import com.ky.serialport.socket.event.ReadDataEvent;
import com.ky.serialport.socket.event.ReconnectFaileEvent;
import com.ky.serialport.socket.event.ReconnectSuccessEvent;
import com.ky.serialport.socket.event.SendOrderEvent;
import com.ky.serialport.socket.event.SendResultEvent;
import com.ky.serialport.utils.MyFunc;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Socket2Activity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn_send)
    Button btnSend;

    private Intent intent;
    private String TAG = this.getClass().getName() + "123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket2);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        intent = new Intent();
        intent.setClass(this, SocketService.class);
        intent.putExtra("IP", "10.10.176.1");
        intent.putExtra("PORT", 4000);
        startService(intent);
    }


    /**
     * Socket连接
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void connectSuccessEvent(ConnectSuccessEvent event) {
        Log.d(TAG, "ConnectSuccessEvent: -------------- >>>>>  Socket连接成功");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void connectFaileEvent(ConnectFaileEvent event) {
        Log.d(TAG, "ConnectFaileEvent: -------------- >>>>>  Socket连接失败");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reconnectSuccessEvent (ReconnectSuccessEvent event) {
        Log.d(TAG, "ReconnectFaileEvent: -------------- >>>>>  Socket重连成功");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reconnectFaileEvent (ReconnectFaileEvent event) {
        Log.d(TAG, "ReconnectFaileEvent: -------------- >>>>>  Socket重连失败");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void connectBreakEvent(ConnectBreakEvent event) {
        Log.d(TAG, "ConnectBreakEvent: -------------- >>>>>  Socket断开");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void readDataEvent(ReadDataEvent event) {
        Log.d(TAG, "ReadDataEvent: -------------- >>>>>  获取服务端数据"+ event.getBytes().length + "   -----    " + MyFunc.ByteArrToHex(event.getBytes()));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void sendResultEvent (SendResultEvent event) {
        Log.d(TAG, "SendResultEvent: -------------- >>>>>  发送命令是否成功" + event.isStatus());
    }


    @OnClick({R.id.btn_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_send:
                EventBus.getDefault().post(new SendOrderEvent(MyFunc.HexToByteArr("010300640C9C017C")));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        stopService(intent);
    }
}
